package com.grandbroad.eim.ui.fragment;

import android.os.Bundle;
import android.support.design.widget.Snackbar;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.LinearLayout;

import com.easy.west.westclient.api.base.bean.VodBean;
import com.easy.west.westclient.api.hotvod.bean.VodOrLiveBean;
import com.easy.west.westclient.api.hotvod.bean.VodOrLiveList;
import com.easy.west.westclient.api.hotvod.event.GetHotVodListEvent;
import com.easy.west.westclient.api.hotvod.event.GetVodOrLiveEvent;
import com.easy.west.westclient.api.vod.bean.ClassList;
import com.easy.west.westclient.api.vod.event.GetAllClassEvent;
import com.grandbroad.eim.R;
import com.grandbroad.eim.adapter.HomePageAdapter;
import com.grandbroad.eim.app.MyApplication;
import com.grandbroad.eim.base.BaseFragment;
import com.grandbroad.eim.ui.activities.SearchActivity;
import com.grandbroad.eim.utils.UiUtlis;

import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;

public class HomeFragment extends BaseFragment {
    @BindView(R.id.top_home_bar)
    Toolbar topHomeBar;
    @BindView(R.id.recycler_view)
    RecyclerView recyclerView;
    @BindView(R.id.base_recyclerview)
    SwipeRefreshLayout baseRecyclerview;
    Unbinder unbinder;
    @BindView(R.id.edit_search)
    EditText editSearch;
    Unbinder unbinder1;
    @BindView(R.id.li_search)
    LinearLayout liSearch;
    Unbinder unbinder2;
    private HomePageAdapter homePageAdapter;
    private List<VodBean> vodBeanList = new ArrayList<>();
    private boolean bananer_load = false;
    private boolean class_load = false;
    private boolean hot_load = false;

    //开启网络请求
    @Override
    protected boolean setEventOpen() {
        return true;
    }

    @Override
    protected void initdata() {
        //初始化数据
        loadData();
    }

    @Override
    protected void initview() {
        liSearch.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                UiUtlis.startAnimActivity((AppCompatActivity) getActivity(),SearchActivity.class,liSearch,"search_edit",true);
            }
        });
        //初始化列表
        initRecyclerview();
        //设置刷新列表
        configSwipRefresh();
    }

    @Override
    protected int setlayout() {
        return R.layout.fragment_home;
    }


    protected void initRecyclerview() {
        homePageAdapter = new HomePageAdapter(getContext(), getChildFragmentManager(), (AppCompatActivity) getActivity());
        recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
        recyclerView.setAdapter(homePageAdapter);
    }

    protected void configSwipRefresh() {
        baseRecyclerview.setRefreshing(true);
        baseRecyclerview.setColorSchemeColors(UiUtlis.getColor(R.color.TextFont));
        baseRecyclerview.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                if (baseRecyclerview.isRefreshing()) {
                    bananer_load = false;
                    hot_load = false;
                    class_load = false;
                    loadData();
                }
            }
        });

    }

    protected void loadData() {
        //请求热门数据(轮播图)
        MyApplication.getMoleclient().getHotVod();
        //请求推荐的4个数据
        MyApplication.getMoleclient().getHotOrLiveNow();
        //获取分类列表
        MyApplication.getMoleclient().getAllClass();
    }

    /**
     * 获取到热门推荐数据
     *
     * @param eva
     */
    @Subscribe(threadMode = ThreadMode.MAIN)
    public void getVodorLive(GetVodOrLiveEvent eva) {
        if (eva.isOk()) {
            VodOrLiveList vodOrLiveList = eva.getBean();
            if (vodOrLiveList.getStatus() == 1) {
                List<VodOrLiveBean> vodOrLiveBeanList = vodOrLiveList.getData();
                homePageAdapter.setHotFourData(vodOrLiveBeanList);
                homePageAdapter.notifyDataSetChanged();
                hot_load = true;
            }
            isrfresh();
        }
    }

    /**
     * 判断所有数据是否加载完毕
     */
    private void isrfresh() {
        if (bananer_load && class_load && hot_load) {
            baseRecyclerview.setRefreshing(false);
        }
    }

    /**
     * 获取所有分类
     *
     * @param eva
     */
    @Subscribe(threadMode = ThreadMode.MAIN)
    public void getAllClass(GetAllClassEvent eva) {
        if (eva.isOk()) {
            ClassList list = eva.getBean();
            if (list.getStatus() == 1) {
                homePageAdapter.setClassList(list.getData());
                homePageAdapter.notifyDataSetChanged();
                class_load = true;
            } else {
                //参数错误活着请求数据不合法
                Snackbar.make(getView(), "错误" + list.getErrorDescrip(), Snackbar.LENGTH_SHORT).show();
            }

        } else {
            //服务器方面的直接返回码
            Snackbar.make(getView(), "错误" + eva.getCodeDescribe(), Snackbar.LENGTH_SHORT).show();
        }
        isrfresh();
    }

    /**
     * 获取所有热门点播
     *
     * @param eva
     */
    @Subscribe(threadMode = ThreadMode.MAIN)
    public void getHotVod(GetHotVodListEvent eva) {
        if (eva.isOk()) {
            vodBeanList = eva.getBean().getData();
            //处理数据
            parseData();
        } else {
            Snackbar.make(getView(), "网络请求失败", Snackbar.LENGTH_SHORT).show();
        }
    }

    private void parseData() {
        //将热度为5的点播拆开
        List<VodBean> veryhot = new ArrayList<>();
        for (VodBean bean : vodBeanList) {
            if (bean.getStick() == 5) {
                veryhot.add(bean);
            }
        }
        //设置入适配器
        homePageAdapter.setBananerData(veryhot);
        homePageAdapter.notifyDataSetChanged();
        bananer_load = true;
        isrfresh();
    }



}
